package com.cgnpc.scp.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cgnpc.cuddemo.utils.IDHelper;
import com.cgnpc.scp.common.exception.ScpException;
import com.cgnpc.scp.common.utils.DateUtil;
import com.cgnpc.scp.common.utils.StringUtil;
import com.cgnpc.scp.purchase.mybatis.entity.CommonProcess;
import com.cgnpc.scp.purchase.mybatis.entity.ContractProcessItem;
import com.cgnpc.scp.purchase.mybatis.entity.PurchaseContractItem;
import com.cgnpc.scp.purchase.mybatis.entity.PurchaseProject;
import com.cgnpc.scp.purchase.mybatis.mapper.client.CommonProcessMapper;
import com.cgnpc.scp.purchase.mybatis.mapper.client.ContractProcessItemMapper;
import com.cgnpc.scp.purchase.mybatis.mapper.client.PurchaseContractItemMapper;
import com.cgnpc.scp.purchase.mybatis.mapper.client.PurchaseProjectMapper;
import com.cgnpc.scp.purchase.service.ICommonProcessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/******************************
 * 用途说明: 流程公共表工具类
 * 作者姓名: chenshijie
 * 创建时间: 2020/09/26 20:14
 ******************************/
@Service
public class CommonProcessServiceImpl implements ICommonProcessService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CommonProcessServiceImpl.class);

    @Autowired
    private CommonProcessMapper commonProcessMapper;

    @Autowired
    ContractProcessItemMapper contractProcessItemMapper;

    @Autowired
    private PurchaseProjectMapper purchaseProjectMapper;

    @Autowired
    private PurchaseContractItemMapper purchaseContractItemMapper;

    @Override
    public void insertCommonInfo(CommonProcess commonProcess) throws Exception {
        List<CommonProcess> commonProcesses = null;
        if(!StringUtil.isNullOrSpace(commonProcess.getProcInstId())) {
//            CommonProcessExample commonProcessExample = new CommonProcessExample();
//            commonProcessExample.createCriteria().andProcInstIdEqualTo(commonProcess.getProcInstId());
//            commonProcesses = commonProcessMapper.selectByExample(commonProcessExample);

            QueryWrapper<CommonProcess> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("proc_inst_id", commonProcess.getProcInstId());
            commonProcesses = commonProcessMapper.selectList(queryWrapper);

        }else if(!StringUtil.isNullOrSpace(commonProcess.getApplyId())) {
//            CommonProcessExample commonProcessExample = new CommonProcessExample();
//            commonProcessExample.createCriteria().andProcInstIdEqualTo(commonProcess.getApplyId());
//            commonProcesses = commonProcessMapper.selectByExample(commonProcessExample);

            QueryWrapper<CommonProcess> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("apply_id", commonProcess.getApplyId());
            commonProcesses = commonProcessMapper.selectList(queryWrapper);
        }else{
            throw new ScpException("commonInfo error.");
        }

        if(commonProcesses!=null && commonProcesses.size()>0) {
            update(commonProcesses,commonProcess);
        }else{
            commonProcess.setId(IDHelper.uuid());
            commonProcessMapper.insert(commonProcess);
            return;
        }

    }

    private void update(List<CommonProcess> commonProcesses,CommonProcess commonProcess) {
        if(commonProcesses != null && commonProcesses.size()>0) {
            commonProcess.setId(commonProcesses.get(0).getId());
            commonProcessMapper.updateById(commonProcess);
        }
    }

    @Override
    public void updateDemandCode(String applyId) throws Exception {
        QueryWrapper<ContractProcessItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("apply_id", applyId);
        List<ContractProcessItem> contractProcessItems = contractProcessItemMapper.selectList(queryWrapper);

        for(int i=0;i<contractProcessItems.size();i++) {
            ContractProcessItem contractProcessItem = contractProcessItems.get(i);
            PurchaseContractItem purchaseContractItem = purchaseContractItemMapper.selectById(contractProcessItem.getItemId());
            PurchaseProject purchaseProject = purchaseProjectMapper.selectById(purchaseContractItem.getProjectId());
            String demandCode = DateUtil.getCurrentYear() + "-" + purchaseProject.getProjectCode() + "-" + String.format("%04d", (i+1));
            contractProcessItem.setDemandCode(demandCode);
            contractProcessItemMapper.updateById(contractProcessItem);
        }

    }

}
